# -*- coding: utf-8 -*-

import time, math
import _feature_  as feature
import _read_xml_ as read_xml
import  _conf_    as conf
from collections import defaultdict



# -----------------------------------------------------------------------------
# global-variables
# -----------------------------------------------------------------------------

max_iter = 1000
period   = 20
epsilon  = 0.001
eta      = 0.1
fn_dev_f    = ''
fn_test_f   = ''
fn_model    = ''
fn_dev_log  = ''
fn_test_log = ''


# -----------------------------------------------------------------------------
# initialize global-variables using _conf_
# -----------------------------------------------------------------------------
def initialize():
    global max_iter, period, epsilon
    global fn_dev_f, fn_test_f, fn_model, fn_dev_log, fn_test_log

    p = conf.params
    k = [ 'max_iterations', 'period', 'epsilon', 'eta' ];
    max_iter = int(p[k[0]])   if p.has_key(k[0]) else 1000
    period   = int(p[k[1]])   if p.has_key(k[1]) else 20
    epsilon  = float(p[k[2]]) if p.has_key(k[2]) else 0.001
    eta      = float(p[k[3]]) if p.has_key(k[3]) else 0.1
    fn_dev_f    = '%s/dev.f'     % conf.log_dir_name
    fn_test_f   = '%s/test.f'    % conf.log_dir_name
    fn_model    = '%s/out.model' % conf.log_dir_name
    fn_dev_log  = '%s/dev.log'   % conf.log_dir_name
    fn_test_log = '%s/test.log'  % conf.log_dir_name


# -----------------------------------------------------------------------------
# get variance.
# -----------------------------------------------------------------------------
def getVariance(list_):
    n = len(list_)
    m = 0.0
    s = 0.0

    for i, x in enumerate(list_):
        x -= m
        m += x/(i+1)
        s += i * x * x / (i+1)

    s = math.sqrt(s/n)
    return s


